package de.breitbeil.jbooking.ui.preferences;

import java.io.IOException;

import javax.swing.JComponent;

import org.springframework.binding.form.FormModel;
import org.springframework.richclient.command.ActionCommand;
import org.springframework.richclient.form.AbstractForm;
import org.springframework.richclient.form.Form;
import org.springframework.richclient.form.FormBuilder;
import org.springframework.richclient.form.builder.TableFormBuilder;
import org.springframework.richclient.preference.FormBackedPreferencePage;
import org.springframework.richclient.preference.PreferencePage;
import org.springframework.richclient.settings.SettingsException;

import de.breitbeil.jbooking.ui.form.SaveCalendarForm;
import de.breitbeil.jbooking.ui.form.SaveCalendarFormObject;

public class CalendarPreferencePage extends FormBackedPreferencePage {

    public static final String ID = "calendarPreferencePage";
    
    private SaveCalendarFormObject formObject;
    
    public CalendarPreferencePage() {
        super(ID);
        
        formObject = new SaveCalendarFormObject();
        
        setTitle(getMessage(ID + ".title"));
        setDescription(getMessage(ID + ".description"));        
    }

    @Override
    protected Form createForm() {
        formObject.init(getSettings());
        
        SaveCalendarForm form = new SaveCalendarForm(formObject);
        form.getFormModel().setValidating(false);
        
        return form;
    }
    
    @Override
    protected void onDefaults() {
        super.onDefaults();
        formObject.fillDefaults(getForm().getFormModel());
    }
    
    @Override
    protected boolean onFinish() {
        getForm().commit();
        SaveCalendarFormObject formObject = (SaveCalendarFormObject)getForm().getFormObject();
        formObject.save(getSettings());
        
        try {
            getSettings().save();
            return true;
        } catch (IOException e) {
            throw new RuntimeException("Can't save settings", e);
        }
    }
    
}
